package com.yc.fruitmarket.biz.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import com.yc.fruitmarket.biz.blogBiz;
import com.yc.fruitmarket.dao.DBHelper;

public class blogBizImpl implements blogBiz {
	private DBHelper db = new DBHelper();

	@Override
	public List<Map<String, String>> getAllBlog(String page, String pageSize, String blogName, String btid) {
		
		int page1 = Integer.parseInt(page);
		int pageSize1 = Integer.parseInt(pageSize);
		String sql = "select blog.bid,blog.title,blog.text,blog.pic,blog.time,count(bval.bvid) as count from blog left join bval on blog.bid = bval.bid where blog.`status` = 1 ";
		if(!"".equals(blogName)) {
			sql += " and blog.title like '%"+ blogName +"%' ";
		}
		if(!"0".equals(btid)) {
			sql += " and blog.btid = "+ btid ;
		}
		sql += " GROUP BY blog.bid ";
		if(!"".equals(blogName)) {
			sql += ",blog.title";
		}
		sql+=" order by blog.bid desc ";
		List<Map<String, String>> list = db.find(sql);
		String num = String.valueOf(list.size());
		sql += " limit ?,? ";
		list = db.find(sql, (page1 - 1)*pageSize1, pageSize1);
		for(int i = 0; i < list.size(); i++) {
			list.get(i).put("blogNum", num);
		}
		return list;
		
	}

	@Override
	public List<Map<String, String>> findBlogByBtid() {
		
		return null;
	}
	
	//获取博客信息
	@Override
	public List<Map<String, String>> getblogL() {
		String sql = "select bid,title,uname,btname,text,blog.status,blog.pic from blog,user,blogtype where blog.uid=user.uid and blog.btid = blogtype.btid";
		return db.find(sql);
	}

	@Override

	public List<Map<String, String>> getHotBlog() {
		String sql = "select blog.bid,blog.title,blog.text,blog.pic,blog.time,count(bval.bvid) as count from blog left join bval on blog.bid = bval.bid where blog.`status` = 1 "+
					"  GROUP BY blog.bid	,blog.title  order by count desc limit 0,3";
		return db.find(sql);
	}

	@Override
	public List<Map<String, String>> findBlogD(String bid) {
		String sql = "select `user`.uname, blog.bid,blog.title,blog.text,blog.pic,blog.time,count(bval.bvid) as count from blog left join bval on blog.bid = bval.bid LEFT JOIN `user` on blog.uid = `user`.uid where blog.`status` = 1 "
				+ " and blog.bid = ? GROUP BY blog.bid	,blog.title ";
		return db.find(sql, bid);
	}

	@Override
	public List<Map<String, String>> getBlogPval(String bid) {
		String sql = "select * from bval,`user` where bval.uid = `user`.uid and bval.bid = ? and bval.status = 1 order by bval.bvid desc";
		return db.find(sql, bid);
	}

	@Override
	public int sendPval(String uid, String bid, String text) {
		String sql = "insert into bval values(null,?,now(),?,?,1)";
		int result = db.doUpdate(sql, uid,text,bid);
		if(result > 0) {
			return 1;
		}else {
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> newBlog() {
		String sql = "select `user`.uname, blog.bid,blog.title,blog.text,blog.pic,blog.time,count(bval.bvid) as count from blog left join bval on blog.bid = bval.bid LEFT JOIN `user` on blog.uid = `user`.uid "
					+ "where blog.`status` = 1 GROUP BY blog.bid ,blog.title  order by blog.bid desc limit 0,5 ";
		return db.find(sql);
	}
	
	//条件查询博客
	public List<Map<String, String>> byselectblogL(String bid, String title, String uname, String start, String end, String btname) {
		String sql = "select bid,title,uname,btname,text,blog.status,blog.pic from blog,user,blogtype where blog.uid=user.uid and blog.btid = blogtype.btid";
		if(!"".equals(bid) && bid != null) {
			sql += " and oid = " + bid;
		}
		if(!"".equals(title) && title != null) {
			sql += " and title like '%"+title+"%'";
		}
		if(!"".equals(uname) && uname != null) {
			sql += " and uname like '%"+uname+"%'";
		}
		if(!"".equals(start)) {
			start = start.substring(0, start.indexOf(" ")) + " 00:00:00";
			end = end.substring(0, end.indexOf(" ")) + " 00:00:00";
			//一天
			if(start.equals(end)) {
				start = start.substring(0, start.indexOf(" "));
				sql += " and time like '"+start+"%'";
			}else {
				//多天
				sql += " and time  between '"+start+"' and '"+end+"'";
			}
		}
		if(!"".equals(btname) && btname != null) {
			sql += " and btname like '%"+btname+"%'";
		}
		return db.find(sql);
	}
	
	//封禁与解禁博客
	@Override
	public int banL(String bid) {
		String sql = "select * from blog where bid = " + bid;
		List<Map<String, String>> list = db.find(sql);
		String status = list.get(0).get("status");
		String sql1 = "";
		int result = -1;
		//解禁
		if("0".equals(status)) {
			sql1 = "update blog set status = 1 where bid = " + bid;
			int a = db.doUpdate(sql1);
			if(a > 0) {
				result = 1;
			}
		}
		//封禁
		if("1".equals(status)) {
			sql1 = "update blog set status = 0 where bid = " + bid;
			int a = db.doUpdate(sql1);
			if(a > 0) {
				result = 0;
			}
		}
		return result;
	}
	
	//批量封禁
	@Override
	public int bansL(String bid) {
		String sql = "update blog set status = 0 where bid = " + bid;
		return db.doUpdate(sql);
	}
	
	//判断
	@Override
	public int selectbanL(String bid) {
		String sql = "select * from blog where bid = " + bid;
		List<Map<String, String>> list = db.find(sql);
		return Integer.parseInt(list.get(0).get("status"));
	}
	
	//今日博客篇数
	@Override
	public int sumblog() {
		LocalDate today = LocalDate.now();    
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");  
        String formattedDate = today.format(formatter);
		String sql = "select * from blog where time like '"+formattedDate+"%'";
		List<Map<String, String>> list = db.find(sql);
		return list.size();
	}
	
}
